<?php
/*
+ ---------------------------------------------------------------------- +
| e107Shop - An e-commerce plugin for the e107 content management system |
|                                                                        |
| ByOrder Europe 2007-2008                                               |
| http://e107shop.com                                                    |
| patrick@byorder.nl                                                     |
|                                                                        |
| Released under the terms and conditions of the LGPL                    |
|                                                                        |
| $Revision: 1.0                                                         |
| $Date: 2008/08/20                                                      |
| $Author: Bieleke                                                       |
+ ---------------------------------------------------------------------- +
*/

function e107shop_display_category_list2() {
    global $e107shop_db_name, $sql, $ns;
    
    
    if(!$sql->db_Select($e107shop_db_name['categories'], "*", "active=1")) {
        return "<center>".lan_admin_nocat_error."</center>";
    } else {    
        
        $display.="<center><form name='form1'>
                    <br />
                    <table class='fborder' width='95%'>
                        <tr>
                            <td class='fcaption' width='95%'>
                                ".lan_manage_cats_title."
                            </td>
                        </tr>
                        <tr>
                            <td class='forumheader2'>
                                ".lan_manage_cats_products.": <select class=\"tbox\" name='g' id='g'>
                                <optgroup label='".lan_admin_cat_active."'></optgroup>";
                            
        
        while($row=$sql->db_Fetch()) {    
            if($row["catId"]==$_GET["g"]) {
                $display.="<option selected value='".$row["catId"]."'>".$row["catName"]."</option>";
            } else {
                $display.="<option value='".$row["catId"]."'>".$row["catName"]."</option>";
            }
        }
        
        
        //
        // Inactive groups
        //
        $sql->db_Select($e107shop_db_name['categories'], "*", "active<>1");

        $display .="<optgroup label='".lan_admin_cat_inactive."'></optgroup>";
        while($row=$sql->db_Fetch())
        {
            if($row["catId"]==$_GET["g"])
            {
                $display.="<option selected value='".$row["catId"]."'>".$row["catName"]."</option>";
            }
            else
            {
                $display.="<option value='".$row["catId"]."'>".$row["catName"]."</option>";
            }
        }
        
        $display.="</select>&nbsp;&nbsp;";
        $display.="<input class=\"tbox\" type='button' name='a' value='".lan_global_edit."' OnClick=\"window.location='admin_manage_categories.php?a=1&p='+document.form1.g.options[document.form1.g.selectedIndex].value\">&nbsp;&nbsp;";
        $display.="<input class=\"tbox\" type='button' name='d' value='".lan_global_delete."' OnClick=\"window.location='admin_manage_categories.php?a=2&p='+document.form1.g.options[document.form1.g.selectedIndex].value\">";

        $display.="</form>";
        $display.="</td></tr></table>";
    }
    
    return $display;
}


function e107shop_manage_categories($action, $cat) {

    global $e107shop_db_name, $sql, $ns;

    if($action==1) {
        //
        //edit the product
        //
        $sql->db_Select($e107shop_db_name['categories'], "*", "catId=".$cat);
        while($row=$sql->db_Fetch()) {
            //
            // Store the fields in variables so they can be
            // used in the form, without having to write out the
            // form twice.
            //
            $cat_image = $row["catimg"];
            $cat_name = $row["catName"];
            $cat_desc = $row["catDesc"];
            $parent_id = $row["parid"];
            $cat_order = $row["catord"];
            $cat_active = $row["active"];
            $cat_id = $row["catId"];
            $cat_items = $row["items"];
            $category_class = $row["category_class"];
            $category_order_class = $row["category_order_class"];                        
        }
        if($cat_active==1) {
            $ischecked = "checked";
        }
        if($cat_items==1)
        {
            $hasitems = "checked";
        }
        $formaction = 1;
    } else {
        $formaction = 2;
    }

    $display .= "<center><form action='".e_SELF."' method='POST' name='catform' ID='catform'>
                <br />
                <table width='95%' class='fborder'>
                    <tr>    
                        <td class='forumheader2' width='25%'>
                            ".lan_category_name.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='cat_name' value='".$cat_name."'>
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='25%'>
                            ".lan_admin_cat_parent.": 
                        </td>
                        <td class='forumheader3'>
                            ".e107shop_admin_get_parents($cat_id, $parent_id)."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='25%'>
                            ".lan_admin_cat_order.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='text' name='cat_order' value='".$cat_order."' />
                            &nbsp;&nbsp;&nbsp;".lan_admin_position_help."
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='25%'>
                            ".lan_admin_cat_thumbnail.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type=text name=\"cat_image\" value='".$cat_image."'>&nbsp;
                            <input class=\"tbox\" type=\"button\" name='image' 
                            onClick=\"window.open('../library/admin/category_image_manager.php',
                            'popuppage','width=700,height=500,top=100,left=100, status=yes, scrollbars=yes');\" 
                            value='".lan_admin_select_image."'>
                        </td>
                    </tr>
                                <tr>
                        <td class='forumheader2' width='25%'>
                            ".lan_admin_cat_description.": 
                        </td>
                        <td class='forumheader3'>
                            <div style='float: left;'>
                            <textarea class=\"tbox\" name='cat_description' rows='10' cols='50'>".$cat_desc."</textarea>
                            </div>
                            <div style='float: left;'><br /><br /><br />
                            &nbsp;&nbsp;&nbsp;".lan_admin_cat_description_help1."<br />
                            &nbsp;&nbsp;&nbsp;".lan_admin_cat_description_help2."
                            </div>    
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='25%'>
                            ".lan_admin_cat1_active.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='checkbox' name='active' ".$ischecked.">
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                            ".lan_admin_items_active.": 
                        </td>
                        <td class='forumheader3'>
                            <input class=\"tbox\" type='checkbox' name='items' ".$hasitems.">
                        </td>
                    </tr>
                    <tr>
                        <td class='forumheader2' width='20%'>
                              ".lan_admin_cat_class.":
                        </td>
                        <td class='forumheader3'>
                            ".r_userclass("category_class", $category_class, "off", "public,guest,member,nobody,main,admin,classes")."
                        </td>
                    </tr>                    
                    <tr>
                        <td class='forumheader2' width='20%'>
                              ".lan_admin_cat_order_class.":
                        </td>
                        <td class='forumheader3'>
                            ".r_userclass("category_order_class", $category_order_class, "off", "public,guest,member,nobody,main,admin,classes")."
                        </td>
                    </tr>                    
                    <tr>                    
                        <td class='forumheader' colspan='2'>
                            <input class=\"tbox\" type='hidden' value='".$formaction."' name='faction'>
                            <input class=\"tbox\" type='hidden' value='".$cat_id."' name='catid'><center>";
    
    if ($action == 1) {
        $display .= "<input class=\"tbox\" type='submit' value='".lan_global_update."' name='subbed'>";
    } else {
        $display .= "<input class=\"tbox\" type='submit' value='".lan_global_submit."' name='subbed'>";
    }
    
    $display .= "</center></td></tr></form></table>";
    
    if($action==1) {
        $ns->tablerender("<center>".lan_manage_edit_category."</center>", $display);
    } else {
        $ns->tablerender("<center>".lan_manage_add_category."</center>", $display);
    }
}

function e107shop_admin_get_parents($cat, $parent)
{
    global $e107shop_db_name, $sql;

    $display = "<select class=\"tbox\" name='parent_cat'>
    <option value=''>".lan_admin_no_higher_cat."</option>";
    if($cat){
        $sql->db_Select($e107shop_db_name['categories'], "*", "catId<>".$cat);
    }
    else
    {
        $sql->db_Select($e107shop_db_name['categories'], "*", "");
    }
    while($row=$sql->db_Fetch())
    {
        if($row["catId"]==$parent){
            $display.="<option selected value='".$row["catId"]."'>".$row["catName"]."</option>";
        }
        else
        {
            $display.="<option value='".$row["catId"]."'>".$row["catName"]."</option>";
        }
    }
    $display .="</select>&nbsp;&nbsp;&nbsp;".lan_admin_no_higher_cat_help;

    return $display;
}

function e107shop_admin_process_category($action, $cat, $c_image, $name, 
$cat_order, $cat_description, $c_parent, $active, $items, $category_class, $category_order_class) {
    global $e107shop_db_name, $sql, $ns, $tp;
    if($active!="") {
        $active=1;
    } else {
        $active=2;
    }
    if ($items != "") {
        $items = 1;
    } else {
        $items = 2;
    }
    if ($cat_order=='') $cat_order=0;
    if($action==1) {
        //
        // edit
        //
        $sql->db_Update($e107shop_db_name['categories'], "catimg='".$c_image."', catName='".
        $tp->toDB($name)."', catDesc='".$tp->toDB($cat_description)."', catord='".
        $cat_order."', parid='".$c_parent."', active=".$active.", items=".
        $items.", category_class=".$category_class.", category_order_class= ".$category_order_class." where catId=".$cat);

        $ns->tablerender("<center>".lan_global_status."</center>", 
        "<center>".lan_global_updated."</center>");
    } else {
        if ($name != '') {
            // Insert a new product

            $sql->db_Insert($e107shop_db_name['categories'], "0, '".$tp->toDB($name)."', '".$tp->toDB($cat_description)."', '"
            .$c_parent."', '".$c_image."', ".$cat_order.", ".$active.", ".$items.", ".$category_class.", ".$category_order_class);
        }
    }
}
